package cn.dd.crmweb.modules.sys.service.impl;


import cn.dd.crmweb.common.constans.Constans;
import cn.dd.crmweb.common.exception.ServiceException;
import cn.dd.crmweb.modules.sys.dto.UserDto;
import cn.dd.crmweb.modules.sys.entity.Menu;
import cn.dd.crmweb.modules.sys.entity.User;
import cn.dd.crmweb.modules.sys.mapper.RoleMapper;
import cn.dd.crmweb.modules.sys.mapper.RoleMenuMapper;
import cn.dd.crmweb.modules.sys.mapper.UserMapper;
import cn.dd.crmweb.modules.sys.service.IMenuService;
import cn.dd.crmweb.modules.sys.service.IUserService;
import cn.dd.crmweb.utils.TokenUtils;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private IMenuService menuService;

    /**
     * 登录
     * @param userDto
     * @return
     */
    @Override
    public UserDto login(UserDto userDto) {
        User one = getUserInfo(userDto);
        if (one != null) {
            BeanUtil.copyProperties(one,userDto,true);
            //设置token
            String token = TokenUtils.genToken(one.getId(), one.getPassword());
            userDto.setToken(token);

            String role = one.getRole(); //
            // 设置用户的菜单列表
            List<Menu> roleMenus = getRoleMenus(role);
            userDto.setMenus(roleMenus);
            return userDto;
        }else {
            throw new ServiceException(Constans.CODE_600,"用户名或密码错误");
        }
    }

    /**
     * 注册
     * @param userDto
     * @return
     */
    @Override
    public User register(UserDto userDto) {
        User one = getUserInfo(userDto);
        if (one == null) {
            one = new User();
            BeanUtil.copyProperties(userDto,one,true);
            save(one); //把 copy的用户存到数据库
        }else {
            throw new ServiceException(Constans.CODE_600,"用户名存在");
        }
        return one;
    }
    private User getUserInfo(UserDto userDto){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",userDto.getUsername());
        queryWrapper.eq("password",userDto.getPassword());
        User one;
        try {
            one = getOne(queryWrapper); //从数据库查询用户信息
        }catch (Exception e){
            throw new ServiceException(Constans.CODE_500,"系统错误");
        }
        return one;
    }

    /**
     *   获取当前角色的菜单列表
     * @param roleFlag
     * @return
     */

    private List<Menu> getRoleMenus(String roleFlag) {
        String roleId = roleMapper.selectByFlag(roleFlag);
        //当前角色的所有菜单id集合
        List<String> menuIds = roleMenuMapper.selectByRoleId(roleId);

        //查出系统所有菜单
        List<Menu> menus = menuService.findMenus("");
        //  new 一个最后筛选完成的list
        List<Menu> roleMenus = new ArrayList<>();

        //筛选当前用户角色的菜单
        for (Menu menu : menus) {
            if (menuIds.contains(menu.getId())) {
                roleMenus.add(menu);
            }
            List<Menu> children = menu.getChildren();
            //移除 children 里面不在 menuIds 集合中的元素
            children.removeIf(child -> !menuIds.contains(child.getId()));
        }
        return roleMenus;
    }
}
